package com.erp.erp_servers.Customer2Contact.imp;

import com.erp.erp_dao.Customer2Contact.ICustomer2ContactDAO;
import com.erp.erp_entitys.Customer2Contact.Customer2ContactEntity;
import com.erp.erp_entitys.Customer2Contact.Customer2ContactExample;
import com.erp.erp_entitys.Customer2Contact.Customer2ContactVoEntity;
import com.erp.erp_entitys.Email.email_custor;
import com.erp.erp_servers.Customer2Contact.ICustomer2ContactService;
import com.erp.erp_servers.Email.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpCustomerContactService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-06-29 20:05
 * @Version 1.0
 */
@Service
public class ImpCustomer2ContactService implements ICustomer2ContactService {
    @Autowired
    ICustomer2ContactDAO customer2ContactDAO;
    @Autowired
    EmailService emailService;
    @Override
    public long countByExample(Customer2ContactExample example) {
        return customer2ContactDAO.countByExample(example);
    }

    @Override
    public int deleteByExample(Customer2ContactExample example) {
        return customer2ContactDAO.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByExample2(Customer2ContactExample example, List<Customer2ContactEntity> list) {
        int num= customer2ContactDAO.deleteByExample(example);
        List<email_custor> email_custors=new ArrayList<>();
        if(num >0){
            for(Customer2ContactEntity cce:list){
                email_custor ec=new email_custor();
                ec.setId(cce.getId());;
                ec.setCode(cce.getId().toString());
                ec.setHandle(2);
                ec.setCodeClass(1);
                ec.setName(cce.getName());
                ec.setStatue(0);
                ec.setOldEmail(cce.getEmail());
                ec.setNewEmail(cce.getEmail());
                email_custors.add(ec);
            }
            boolean tf = emailService.checkUserBox(email_custors);

        }
        return num;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return customer2ContactDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Customer2ContactEntity record) {
        return customer2ContactDAO.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(Customer2ContactEntity record) {
        int num= customer2ContactDAO.insertSelective(record);
        if (record.getIsDefault()==1){
            Customer2ContactExample example=new Customer2ContactExample();
            Customer2ContactExample.Criteria criteria = example.createCriteria();
            criteria.andCompanyEqualTo(record.getCompany());
            List<Customer2ContactEntity> entities = customer2ContactDAO.selectByExample(example);
            for (Customer2ContactEntity p: entities) {
                //把其他的默认改成非默认
                if (!record.getId().equals(p.getId()) && p.getIsDefault()==1){
                    p.setIsDefault(0);
                    customer2ContactDAO.updateByPrimaryKeySelective(p);
                }
            }
        }

        if(num >0){
            Customer2ContactExample example=new Customer2ContactExample();
            Customer2ContactExample.Criteria criteria = example.createCriteria();
            criteria.andEmailEqualTo(record.getEmail());
            criteria.andCompanyEqualTo(record.getCompany());
            criteria.andNameEqualTo(record.getName());
             Customer2ContactEntity cce= customer2ContactDAO.selectByExample(example).get(0);
            List<email_custor> email_custors=new ArrayList<>();

            email_custor ec=new email_custor();
            ec.setId(cce.getId());;
            ec.setCode(cce.getId().toString());
            ec.setHandle(0);
            ec.setCodeClass(1);
            ec.setName(cce.getName());
            ec.setStatue(0);
            ec.setOldEmail(cce.getEmail());
            ec.setNewEmail(cce.getEmail());
            email_custors.add(ec);

            boolean tf = emailService.checkUserBox(email_custors);
        }
        return num;
    }

    @Override
    public List<Customer2ContactEntity> selectByExample(Customer2ContactExample example) {
        return customer2ContactDAO.selectByExample(example);
    }

    @Override
    public List<Customer2ContactVoEntity> query(String name, String email, String salesman) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("name",name);
        map.put("email",email);
        map.put("salesman",salesman);
        return customer2ContactDAO.query(map);
    }

    @Override
    public List<Customer2ContactVoEntity> query(String company) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("company",company);
        return customer2ContactDAO.query2(map);
    }


    @Override
    public Customer2ContactEntity selectByPrimaryKey(Integer id) {
        return customer2ContactDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Customer2ContactEntity record, Customer2ContactExample example) {
        return customer2ContactDAO.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(Customer2ContactEntity record, Customer2ContactExample example) {
        return customer2ContactDAO.updateByExample(record,example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(Customer2ContactEntity record) {
        Customer2ContactEntity cce = customer2ContactDAO.selectByPrimaryKey(record.getId());
        int num= customer2ContactDAO.updateByPrimaryKeySelective(record);

        if (record.getIsDefault()==1){
            Customer2ContactExample example=new Customer2ContactExample();
            Customer2ContactExample.Criteria criteria = example.createCriteria();
            criteria.andCompanyEqualTo(record.getCompany());
            List<Customer2ContactEntity> entities = customer2ContactDAO.selectByExample(example);
            for (Customer2ContactEntity p: entities) {
                //把其他的默认改成非默认
                if (!record.getId().equals(p.getId()) && p.getIsDefault()==1){
                    p.setIsDefault(0);
                    customer2ContactDAO.updateByPrimaryKeySelective(p);
                }
            }
        }

        if(num > 0){
            List<email_custor> email_custors=new ArrayList<>();
            email_custor ec=new email_custor();
            ec.setId(cce.getId());;
            ec.setCode(cce.getId().toString());
            ec.setHandle(1);
            ec.setCodeClass(1);
            ec.setName(cce.getName());
            ec.setStatue(0);
            ec.setOldEmail(cce.getEmail());
            ec.setNewEmail(record.getEmail());
            email_custors.add(ec);
            boolean tf = emailService.checkUserBox(email_custors);
        }

        return num;
    }

    @Override
    public int updateByPrimaryKey(Customer2ContactEntity record) {
        return customer2ContactDAO.updateByPrimaryKey(record);
    }

    @Override
    public List<Customer2ContactEntity> selectByDim(String dim) {
        return customer2ContactDAO.selectByDim(dim);
    }
}